<template>
  <div>
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <span style="text-align: left">基础信息</span>
      <br />
      <br />
      <el-row :gutter="24">
        <el-col :span="12" :offset="0">
          <el-form-item label="支出单号" prop="TheInvoiceNumber">
            <el-input v-model="ruleForm.TheInvoiceNumber" />
          </el-form-item>
          <el-form-item label="支出类型" prop="ExpenditureTypeId">
            <el-select
              v-model="ruleForm.ExpenditureTypeId"
              placeholder="请选择"
              style="width: 240px"
            >
              <el-option
                v-for="item in ExpenditureTypeList"
                :key="item.ExpenditureTypeId"
                :label="item.ExpenditureTypeName"
                :value="item.ExpenditureTypeId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="Remark">
            <el-input v-model="ruleForm.Remark" type="textarea" />
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="支出主题" prop="SupportingTopic">
            <el-input v-model="ruleForm.SupportingTopic" />
          </el-form-item>
          <el-form-item label="支出日期" prop="SupportDate">
            <el-date-picker
              v-model="ruleForm.SupportDate"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
          <el-form-item label="付款金额" prop="PaymentAmount">
            <el-input v-model="ruleForm.PaymentAmount" /> </el-form-item
        ></el-col>
      </el-row>
      <span style="text-align: left">收款方信息</span>
      <br />
      <br />
      <el-row :gutter="24">
        <el-col :span="12" :offset="0">
          <el-form-item label="收款方" prop="Payee">
            <el-input v-model="ruleForm.Payee" />
          </el-form-item>
          <el-form-item label="账户名称" prop="AccountName">
            <el-input v-model="ruleForm.AccountName" />
          </el-form-item>
          <el-form-item label="网点名称" prop="NetworkName">
            <el-input v-model="ruleForm.NetworkName" /> </el-form-item
        ></el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="开户银行" prop="DepositBank">
            <el-input v-model="ruleForm.DepositBank" />
          </el-form-item>
          <el-form-item label="银行账号" prop="BankAccount">
            <el-input v-model="ruleForm.BankAccount" /> </el-form-item
        ></el-col>
      </el-row>
      <span style="text-align: left">采购订单</span>
      <br />
      <br />
      <el-row :gutter="24">
        <el-col :span="12" :offset="0">
          <el-form-item label="采购订单主题" prop="PurchaseOrderSubject">
            <el-input v-model="ruleForm.PurchaseOrderSubject" />
          </el-form-item>
          <el-form-item label="入库状态" prop="StorageStatus">
            <el-input v-model="ruleForm.StorageStatus" />
          </el-form-item>
          <el-form-item label="已收款金额" prop="AmountPaid">
            <el-input v-model="ruleForm.AmountPaid" /> </el-form-item
        ></el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="采购日期" prop="PurchaseDate">
            <el-date-picker
              v-model="ruleForm.PurchaseDate"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
          <el-form-item label="总金额" prop="AggregateAmount">
            <el-input v-model="ruleForm.AggregateAmount" /> </el-form-item
        ></el-col>
      </el-row>
      <span style="text-align: left">附件</span>
      <br />
      <br />
      <el-upload
        class="avatar-uploader"
        action="http://localhost:5055/api/Sale/GetImage"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
      >
        <img
          v-if="ruleForm.Attachment"
          :src="ruleForm.Attachment"
          class="avatar"
          prop="Attachment"
          style="width: 100; height: 100"
        />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
      <br />
      <br />
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button>关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { ElMessage, FormInstance, FormRules, ElUpload } from "element-plus";
import axios from "axios";
import { UploadFilled } from "@element-plus/icons-vue";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";
import { useRouter, useRoute } from "vue-router";
const Router = useRouter();
const handleAvatarSuccess: UploadProps["onSuccess"] = (response) => {
  ruleForm.Attachment = response;
};
interface RuleForm {
  TheInvoiceNumber: string;
  ExpenditureTypeId: string;
  Remark: string;
  SupportingTopic: string;
  SupportDate: string;
  PaymentAmount: string;
  Payee: string;
  AccountName: string;
  NetworkName: string;
  DepositBank: string;
  BankAccount: string;
  PurchaseOrderSubject: string;
  StorageStatus: string;
  AmountPaid: string;
  PurchaseDate: string;
  AggregateAmount: string;
  Attachment: string;
}

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  TheInvoiceNumber: "",
  ExpenditureTypeId: "",
  Remark: "",
  SupportingTopic: "",
  SupportDate: "",
  PaymentAmount: "",
  Payee: "",
  AccountName: "",
  NetworkName: "",
  DepositBank: "",
  BankAccount: "",
  PurchaseOrderSubject: "",
  StorageStatus: "",
  AmountPaid: "",
  PurchaseDate: "",
  AggregateAmount: "",
  Attachment: "",
});
const ExpenditureTypeList = ref([]);
const GetExpenditureTypeList = () => {
  axios.get("AdditionalExpenditure/GetExpenditureType").then((res) => {
    ExpenditureTypeList.value = res.data;
  });
};
const rules = reactive<FormRules<RuleForm>>({
  TheInvoiceNumber: [
    { required: true, message: "请输入订单号", trigger: "blur" },
  ],
  ExpenditureTypeId: [
    {
      required: true,
      message: "请选择",
      trigger: "change",
    },
  ],
  Remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
  SupportingTopic: [
    { required: true, message: "请输入支出主题", trigger: "blur" },
  ],
  SupportDate: [
    {
      type: "date",
      required: true,
      message: "请选择时间",
      trigger: "change",
    },
  ],
  PaymentAmount: [
    { required: true, message: "请输入付款金额", trigger: "blur" },
  ],
  Payee: [{ required: true, message: "请输入收款方", trigger: "blur" }],
  AccountName: [{ required: true, message: "请输入账户名称", trigger: "blur" }],
  NetworkName: [{ required: true, message: "请输入网点名称", trigger: "blur" }],

  DepositBank: [{ required: true, message: "请输入开户银行", trigger: "blur" }],
  BankAccount: [{ required: true, message: "请输入银行账号", trigger: "blur" }],
  PurchaseOrderSubject: [
    { required: true, message: "请输入采购订单主题", trigger: "blur" },
  ],
  StorageStatus: [
    { required: true, message: "请输入采购状态", trigger: "blur" },
  ],

  AmountPaid: [
    { required: true, message: "请输入已收款金额", trigger: "blur" },
  ],
  PurchaseDate: [
    { type: "date", required: true, message: "请选择时间", trigger: "change" },
  ],
  AggregateAmount: [
    { required: true, message: "请输入总金额", trigger: "blur" },
  ],
  Attachment: [{ required: true, message: "请选择图片", trigger: "blur" }],

  // count: [
  //   {
  //     required: true,
  //     message: "Please select Activity count",
  //     trigger: "change",
  //   },
  // ],
  // SupportDate: [
  //   {
  //     type: "date",
  //     required: true,
  //     message: "Please pick a date",
  //     trigger: "change",
  //   },
  // ],
  //. date2: [
  //   {
  //     type: "date",
  //     required: true,
  //     message: "Please pick a time",
  //     trigger: "change",
  //   },
  // ],
  // type: [
  //   {
  //     type: "array",
  //     required: true,
  //     message: "Please select at least one activity type",
  //     trigger: "change",
  //   },
  // ],
  // resource: [
  //   {
  //     required: true,
  //     message: "Please select activity resource",
  //     trigger: "change",
  //   },
  // ],
  // desc: [
  //   { required: true, message: "Please input activity form", trigger: "blur" },
  // ],
});

const submitForm = () => {
  axios
    .post("AdditionalExpenditure/AddadditionalExpenditure", ruleForm)
    .then((res) => {
      if (res.data > 0) {
        ElMessage.success("添加成功！");
        Router.push(`/UserList/showitionalExpenditure`);
      }
    });
};
const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
GetExpenditureTypeList();
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
